# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 18:34:30 2020

This script is used for visualized 2 compounds EC.

@author: Saki
"""

import os
import csv

import numpy as np
import matplotlib.pyplot as plt

output_dir = r'D:\Documents\ANU\PHD\2020\kangyuan\reinford_dataset\output'

filenames = os.listdir(output_dir)
# Filter all the EC csv.
filenames = [filename for filename in filenames if filename.lower().startswith('ec_')]
# Change the plot axis.
plt.xscale('log')
plt.yscale('log')
# Find the minimal value.
min_x_c = []
min_y_c = []
# Loop for all the files.
for filename in filenames:
    # Prepare the list of x and y.
    x_c = []
    y_c = []
    # Prepare the best efficient configuration of x and y.
    min_x = -1
    min_y = -1
    min_total = -1
    
    def calculate_total(x, y):
        return np.sqrt(x * x + y * y)
    
    def update_minimal(new_x, new_y):
        global min_x
        global min_y
        global min_total
        new_total = calculate_total(new_x, new_y)
        if new_total < min_total:
            min_x = new_x
            min_y = new_y
            min_total = new_total
    
    with open(os.path.join(output_dir, filename), 'r') as ec_csv_file:
        ec_reader = csv.reader(ec_csv_file, delimiter=',')
        for ec_row in ec_reader:
            val_x_c, val_y_c = float(ec_row[0]), float(ec_row[1])
            # Read the X and Y coordiates.
            x_c.append(val_x_c)
            y_c.append(val_y_c)
            if min_total == -1:
                # Save the value first.
                min_x, min_y = val_x_c, val_y_c
                min_total = calculate_total(min_x, min_y)
            else:
                # Check the minimal.
                update_minimal(val_x_c, val_y_c)
    # Print the value in diagram.
    plt.scatter(x_c, y_c)
    # Append the minimal result.
    if min_total != -1:
        min_x_c.append(min_x)
        min_y_c.append(min_y)
plt.plot(min_x_c, min_y_c, '-o')
plt.show()
